﻿using System;
using System.Collections.Generic;
using System.IO;

namespace iwpm.optimization
{
    public class GrandCouleeTestBusWindList : List<GrandCouleeTestBusWind>, IScenarioTestable
    {
        private bool _error;

        public bool ErrorOccurred
        {
            get
            {
                return _error;
            }
        }

        public GrandCouleeTestBusWindList()
            : base()
        {
        }

        public GrandCouleeTestBusWindList(IEnumerable<GrandCouleeTestBusWind> collection)
            : base(collection)
        {
        }

        // Methods 
        public void SolveAll()
        {
            int procID = System.Diagnostics.Process.GetCurrentProcess().Id;
            string outfile = Path.GetDirectoryName(
                Path.GetFullPath(this.GetType().Assembly.Location))
                + "\\Scenario_Log_" + procID + ".csv";
            StreamWriter sw = new StreamWriter(outfile);
            sw.WriteLine("Name,Time (min)");
            foreach (IScenarioSolvable scen in this)
            {
                scen.Solve();
                if (!_error)
                    _error = scen.ErrorOccurred;
                sw.WriteLine(scen.Name + "," + scen.SolutionTime.TotalMinutes.ToString());
                sw.Flush();
            }
            sw.Close();
        }

        public void WriteToFile(string file)
        {
            try
            {
                StreamWriter sw = new StreamWriter(file);
                foreach (IScenarioSolvable scen in this)
                    sw.WriteLine(scen.ToString());
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to write to file: " + ex.ToString());
            }
        }

        public static GrandCouleeTestBusWindList FromStrings(string[] s)
        {
            GrandCouleeTestBusWind model; 
            List<GrandCouleeTestBusWind> models = new List<GrandCouleeTestBusWind>();
            for (int i = 0; i < s.Length; i++)
            {
                model = GrandCouleeTestBusWind.FromString(s[i]);
                if (model != null)
                    models.Add(model);
            }
            return new GrandCouleeTestBusWindList(models);
        }

        public static GrandCouleeTestBusWindList FromFile(string file)
        {
            return FromFile(file, "\\");
        }

        public static GrandCouleeTestBusWindList FromFile(string file, string commentStart)
        {
            StreamReader sr = new StreamReader(file);
            List<string> all = new List<string>(sr.ReadToEnd().Split(new char[] { '\n' },
                StringSplitOptions.RemoveEmptyEntries));
            sr.Close();
            for (int i = all.Count - 1; i >= 0; i--)
            {
                all[i] = all[i].Trim();
                if (all[i] == "" || all[i].StartsWith(commentStart))
                    all.RemoveAt(i);
            }
            return FromStrings(all.ToArray());
        }

    }
}
